package com.loan.system.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.common.helper.LoginHelper;
import com.loan.common.utils.DateUtils;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.CollectionOrderHandleEntity;
import com.loan.system.domain.OverConfigEntity;
import com.loan.system.mapper.CollectionOrderHandleMapper;
import com.loan.system.service.CollectionOrderHandleService;
import com.loan.system.service.OverConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shiguang on 2022/11/8 9:30
 */
@Service
@RequiredArgsConstructor
public class CollectionOrderHandleServiceImpl implements CollectionOrderHandleService {

    private final CollectionOrderHandleMapper baseMapper;

    private final OverConfigService overConfigService;

    @Override
    public void save(ApplyEntity entity, OverConfigEntity overConfigEntity, Boolean isSystem) {
        //判断该订单是否有记录
        CollectionOrderHandleEntity collectionOrderHandle = baseMapper.selectOne(new QueryWrapper<CollectionOrderHandleEntity>()
                .eq("order_id", entity.getId())
                .orderByDesc("id")
                .last("limit 1"));
        CollectionOrderHandleEntity collectionOrderHandleEntity = new CollectionOrderHandleEntity();
        if (ObjectUtil.isNotNull(collectionOrderHandle)) {
            collectionOrderHandle.setDelFlag("2");
            collectionOrderHandle.setUpdateTime(new Date());
            collectionOrderHandle.setStatus("fail_end");
            baseMapper.updateById(collectionOrderHandle);
            collectionOrderHandleEntity.setType(isSystem ? "timeout_switch_other" : "switch_other");
        }else {
            collectionOrderHandleEntity.setType("init");
        }
        collectionOrderHandleEntity.setOrderId(entity.getId());
        collectionOrderHandleEntity.setStaffId(entity.getCollectionBy());
        collectionOrderHandleEntity.setAgentId(entity.getCompanyId());
        collectionOrderHandleEntity.setAssignStaffId(isSystem ? 0L : LoginHelper.getUserId());
        collectionOrderHandleEntity.setTotalAmount(entity.getRealApplyMoney() + entity.getOverMoney() - entity.getAlreadyRepayMoney());
        collectionOrderHandleEntity.setCallbackAmount(0);
        collectionOrderHandleEntity.setStatus("init");
        collectionOrderHandleEntity.setDelFlag("0");

        collectionOrderHandleEntity.setMinDays(overConfigEntity.getMinDay());
        collectionOrderHandleEntity.setMaxDays(overConfigEntity.getMaxDay());
        collectionOrderHandleEntity.setCreateTime(new Date());
        baseMapper.insert(collectionOrderHandleEntity);
    }

    @Override
    public void saveByApply(ApplyEntity applyEntity) {
        long days = DateUtil.between(DateUtils.parseDate(applyEntity.getRepaymentTime()), DateUtil.beginOfDay(new Date()), DateUnit.DAY);
        if (days != 0 && DateUtils.parseDate(applyEntity.getRepaymentTime()).after(DateUtil.beginOfDay(new Date()))) {
            days = 0 - days;
        }
        List<OverConfigEntity> overConfigEntityList = overConfigService.selectList(applyEntity.getCompanyId());
        for (OverConfigEntity entity : overConfigEntityList) {
            if (days >= entity.getMinDay() && days <= entity.getMaxDay()) {
                save(applyEntity, entity, false);
                break;
            }
        }
    }

    @Override
    public CollectionOrderHandleEntity selectByApply(ApplyEntity applyEntity) {
        return baseMapper.selectOne(new QueryWrapper<CollectionOrderHandleEntity>()
                .eq("order_id",applyEntity.getId())
                .eq("staff_id",applyEntity.getCollectionBy())
                .orderByDesc("id")
                .last("limit 1")
        );
    }

    @Override
    public void update(CollectionOrderHandleEntity collectionOrderHandleEntity) {
        baseMapper.updateById(collectionOrderHandleEntity);
    }

}
